using IStrong.EC.Web.Controllers;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using IStrong.EC.ExtensionMethods;
using IStrong.EC.WebApi.Results.Repository;
using IStrong.EC.WebApi.Results.Entity;
using Newtonsoft.Json.Linq;
using Microsoft.IdentityModel.Tokens;
using Microsoft.AspNetCore.Hosting;

namespace IStrong.EC.WebApi.Results.Controllers
{
    /// <summary>
    /// Api控制器模板
    /// </summary>
    [Route("api/[controller]")]
    [ApiController]
    public class CustomerController : BaseController
    {
        /// <summary>
        /// 日志
        /// </summary>
        private readonly ILogger<CustomerController> _logger;

        /// <summary>
        /// 配置
        /// </summary>
        private readonly IConfiguration _config;

        /// <summary>
        /// 服务合同
        /// </summary>
        private readonly Customer _customer;

        /// <summary>
        /// 
        /// </summary>
        private readonly Repository.Utility _unitl;

        /// <summary>
        /// 
        /// </summary>
        public readonly IHostingEnvironment _hostingEnvironment;
        /// <summary>
        /// 注入服务
        /// </summary>
        /// <param name="logger"></param>
        /// <param name="config"></param>
        /// <param name="customer"></param>
        /// <param name="hostingEnvironment"></param>
        /// <param name="unitl"></param>
        public CustomerController(ILogger<CustomerController> logger, IConfiguration config, Customer customer, IHostingEnvironment hostingEnvironment, Repository.Utility unitl)
        {
            _logger = logger;
            _config = config;
            _customer = customer;
            _hostingEnvironment = hostingEnvironment;
            _unitl = unitl;
        }

        #region GET
        /// <summary>
        /// 获取老客户信息
        /// </summary>
        /// <param name="CustomerName">客户名称</param>
        /// <param name="County">行政区划</param>
        /// <param name="Territory">责任区域</param>
        /// <param name="TrackPepole">执行人</param>
        /// <param name="CustomerId">客户编码</param>
        /// <param name="PageSize"></param>
        /// <param name="PageIndex"></param>
        /// <returns></returns>
        [HttpGet("oldinfo")]
        public JsonResult GetCustomerOldInfos(
            string CustomerName, 
            string CustomerId,
            string County, 
            string Territory, 
            string TrackPepole,
            int PageSize = 50,
            int PageIndex = 1
            )
        {
            try
            {
                string[] newTerritory = null;
                if (null != Territory) newTerritory = Territory.Split(",");

                var data = _customer.GetCustomerOldInfos(new
                {
                    CustomerName,
                    County,
                    newTerritory,
                    TrackPepole,
                    CustomerId
                });

                var newData = data.Where(d => d.RowId > ((PageIndex - 1) * PageSize) && d.RowId <= PageSize * PageIndex);

                var result = new
                {
                    pageSize = PageSize,
                    pageIndex = PageIndex,
                    totalCount = data.Count(),
                    data = newData
                };
                return Json(result);
            }
            catch (Exception ex)
            {
                return Json(ex);
            }
        }

        /// <summary>
        /// 根据客户ID获取跟踪记录
        /// </summary>
        /// <param name="CustomerId">客户编码</param>
        /// <param name="PageSize"></param>
        /// <param name="PageIndex"></param>
        /// <returns></returns>
        [HttpGet("records")]
        public JsonResult GetCustomerOldRecordsById(
            string CustomerId,
            int PageSize = 10,
            int PageIndex = 1
            )
        {
            try
            {
                var data = _customer.GetCustomerOldRecordsById(new
                {
                    CustomerId
                });

                var newData = data.Where(d => d.RowId > ((PageIndex - 1) * PageSize) && d.RowId <= PageSize * PageIndex);

                var result = new
                {
                    pageSize = PageSize,
                    pageIndex = PageIndex,
                    totalCount = data.Count(),
                    data = newData
                };
                return Json(result);
            }
            catch (Exception ex)
            {
                return Json(ex);
            }
        }

        #endregion GET

        #region POST

        /// <summary>
        /// 新增/修改客户信息
        /// </summary>
        /// <param name="CustomerOldInfo"></param>
        /// <returns></returns>
        [HttpPost("oldinfo")]
        public JsonResult AddCustomerOldInfo(CustomerOldInfo CustomerOldInfo)
        {
            try
            {
                if (null == CustomerOldInfo.CustomerId) CustomerOldInfo.CustomerId = Guid.NewGuid().ToString("N").Substring(0, 8);

                var data = _customer.AddCustomerOldInfo(CustomerOldInfo)> 0 ? true : false;

                return Json(data);
            }
            catch (Exception ex)
            {
                return Json(ex);
            }
        }

        /// <summary>
        /// 新增客户跟踪记录
        /// </summary>
        /// <param name="CustomerOldRecord"></param>
        /// <returns></returns>
        [HttpPost("record")]
        public JsonResult AddCustomerOldRecord(CustomerOldRecord CustomerOldRecord)
        {
            try
            {
                var data = _customer.AddCustomerOldRecord(CustomerOldRecord) > 0 ? true : false;

                return Json(data);
            }
            catch (Exception ex)
            {
                return Json(ex);
            }
        }
        #endregion POST

        #region DELETE
        /// <summary>
        /// 根据客户ID删除记录
        /// </summary>
        /// <param name="CustomerId"></param>
        /// <returns></returns>
        [HttpDelete("oldinfo")]
        public JsonResult DelCustomerOldInfoById(string CustomerId)
        {
            try
            {
                var data = _customer.DelCustomerOldInfoById(new { CustomerId }) > 0 ? true : false;
                return Json(data);
            }
            catch (Exception ex)
            {
                return Json(ex);
            }
        }

        /// <summary>
        /// 根据记录ID删除跟踪记录
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        [HttpDelete("record")]
        public JsonResult DelCustomerOldRecordById(string Id)
        {
            try
            {
                var data = _customer.DelCustomerOldRecordById(new { Id }) > 0 ? true : false;
                return Json(data);
            }
            catch (Exception ex)
            {
                return Json(ex);
            }
        }
        #endregion DELETE

        #region   数据导出

        #endregion  数据导出
    }
}